<template>
  <div class="cgqlist">
    <ConTitle :info="info"></ConTitle>
    <el-tabs type="border-card" @tab-click="handleClick">
      <el-tab-pane label="所有设备" name="all"></el-tab-pane>
      <el-tab-pane label="LoRaWAN" name="LoRaWAN"></el-tab-pane>
      <el-tab-pane label="NB-loT" name="NB-loT"></el-tab-pane>
      <el-tab-pane label="4G/2G" name="4G/2G"></el-tab-pane>
      <el-tab-pane label="LoRaPP" name="LoRaPP"></el-tab-pane>
      <el-form ref="form" :model="query" label-width="80px">
        <el-form-item label="EUI">
          <el-input v-model="query.eui"></el-input>
        </el-form-item>
        <el-form-item label="设备群组">
          <el-select v-model="query.online" placeholder="请选择">
            <el-option
              v-for="item in options"
              :label="item.label"
              :value="item.value"
              :key="item.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="在线状态">
          <el-select v-model="query.online" placeholder="请选择">
            <el-option
              v-for="item in options"
              :label="item.label"
              :value="item.value"
              :key="item.label"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="绑定时间">
          <el-date-picker
            v-model="query.date"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button-group>
            <el-button
              type="primary"
              @click="onSubmit"
              icon="el-icon-search"
              size="small"
              >搜索</el-button
            >
            <el-button
              type="primary"
              icon="el-icon-delete"
              size="small"
              @click="deleteSearch"
              >清除条件</el-button
            >
            <el-button
              type="primary"
              icon="el-icon-refresh"
              size="small"
              @click="onRefresh"
              >刷新</el-button
            ><span class="search-end">搜索结果：{{ total }}</span>
          </el-button-group>
        </el-form-item>
      </el-form>
    </el-tabs>

    <el-table :data="cgqList" stripe style="width: 100%">
      <el-table-column prop="eui" label="EUI" width="180"> </el-table-column>
      <el-table-column prop="name" label="设备名称" width="180">
      </el-table-column>
      <el-table-column prop="nums" label="传感器通道"> </el-table-column>
      <el-table-column prop="devGroup" label="设备群组"> </el-table-column>
      <el-table-column prop="loop" label="上报周期（分钟）"> </el-table-column>
      <el-table-column prop="mhz" label="频率(MHz)"> </el-table-column>
      <el-table-column label="在线状态">
        <template slot-scope="scope">
          {{ scope.row.online | onlineType }}
        </template>
      </el-table-column>
      <el-table-column label="绑定时间">
        <template slot-scope="scope">
          {{ scope.row.bTime | bTimeType }}
        </template>
      </el-table-column>
    </el-table>
    <div class="cgqlist-page">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.currPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="query.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import ConTitle from "@/components/ConTitle.vue";
import moment from "moment";
moment.locale("zh-cn");
import { $cgqList } from "@/api";
export default {
  name: "CgqlistView",
  data() {
    return {
      info: {
        path: "设备/",
        text: "传感器列表",
        msg: "展示EUI、名称、状态、数据类型等设备信息，可根据EU、群组、状态、绑定时间等条件筛选具体的传感器节点，并提供移动群组等功能.",
      },
      cgqList: [],
      total: 0, //总数量的默认值
      query: {
        currPage: 1, //当前页码
        pageSize: 5, //每页显示的条目
        eui: "",
        date: [],
      },
      options: [
        { value: true, label: "在线" },
        { value: false, label: "离线" },
        { value: "all", label: "全部" },
      ],
    };
  },
  components: {
    ConTitle,
  },
  computed: {
    dateStr() {
      let d1 = this.query.date[0];
      let d2 = this.query.date[1];
      let time1 = d1 && d1.getTime();
      let time2 = d2 && d2.getTime();
      if (time1 && time2) {
        return time1 + "-" + time2;
      } else {
        return "";
      }
    },
  },
  filters: {
    onlineType(value) {
      return value ? "在线" : "离线";
    },
    bTimeType(value) {
      return moment(value).format("LL");
    },
  },
  mounted() {
    this.getListData(this.query);
  },
  methods: {
    async getListData() {
      var queryStr = { ...this.query };
      if (queryStr != "") {
        queryStr.date = this.dateStr;
      }
      var res = await $cgqList(queryStr);
      console.log(res);
      this.cgqList = res.data.data;
      this.total = res.data.total;
    },
    handleSizeChange(num) {
      console.log(num);
      this.query.pageSize = num;
      this.query.currPage = 1;
      this.getListData();
    },
    handleCurrentChange(curr) {
      console.log(curr);
      this.query.currPage = curr;
      this.getListData();
    },
    onSubmit() {
      console.log("表单提交了");
      this.getListData();
    },
    //清除条件
    deleteSearch() {
      this.query = {
        currPage: 1, //当前页码
        pageSize: 5, //每页显示的条目
        eui: "",
        online: "",
        data: [],
      };
    },
    //页面刷新
    onRefresh() {
      //
      this.$router.go(0);
    },
    handleClick() {
      this.getListData();
    },
  },
};
</script>

<style lang="scss" scoped>
.cgqlist {
  padding: 10px;
  .cgqlist-page {
    width: 100%;
    height: 50px;
    background: #fff;
    text-align: center;
    line-height: 50px;
    padding-top: 20px;
  }
}
.search-end {
  font-size: 12px;
  margin-left: 10px;
  color: #858793;
}
.el-input {
  width: 220px;
}
</style>